<template>
  <div class="item">

    <div class="item-search">
      <el-collapse v-model="activeName">
        <el-collapse-item name="1">
          <template slot="title">
            <el-button type="primary">搜索</el-button>
          </template>
          <div class="item-search-input">
            <el-row>
              <el-col :span="2">
                <el-select v-model="searchData.city" placeholder="状态" class="item-search-input-l" @keyup.enter.native="search">
                  <el-option v-for="item in cityOptions" :label="item.label" :value="item.value" :key="item.value"></el-option>
                </el-select>
              </el-col>
              <el-col :span="3">
                <el-input v-model="searchData.aliUserId" placeholder="userId" class="item-search-input-l" @keyup.enter.native="search"></el-input>
              </el-col>
              <el-col :span="3">
                <el-input v-model="searchData.userName" placeholder="姓名" class="item-search-input-l" @keyup.enter.native="search"></el-input>
              </el-col>
              <el-col :span="3">
                <el-input v-model="searchData.phone" placeholder="电话" class="item-search-input-l" @keyup.enter.native="search"></el-input>
              </el-col>
              <el-col :span="3">
                <el-input v-model="searchData.identityCard" placeholder="证件号" class="item-search-input-l" @keyup.enter.native="search"></el-input>
              </el-col>
              <el-col :span="8">
                <el-date-picker class="item-search-input-l" v-model="dateTimeScope"  type="datetimerange"  placeholder="选择搜索时间范围" align="left" :editable="false" :clearable="false" @keyup.enter.native="search" @input="dateTimeScopeChange"></el-date-picker>
              </el-col>
              <el-col :span="1">
                <el-button type="primary" icon="search" @click="search"></el-button>
              </el-col>
              <el-col :span="1">
                <el-button type="primary" icon="delete" @click="reset"></el-button>
              </el-col>
            </el-row>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>

    <div class="item-table">

      <div class="item-table-title">
        <h2>{{ tableTitle.big }}<span>{{ tableTitle.small }}</span></h2>
        <div class="item-table-add"></div>
      </div>

      <div class="item-table-main">

        <el-table :data="tableData" border>
          <el-table-column v-for="(item,index) in table" :label="item.label" :key="index" :sortable="item.sortable ? true : false" :prop="item.name">
            <template scope="scope">
              <span :title="scope.row[item.name]">{{ scope.row[item.name] }}</span>
            </template>
          </el-table-column>
        </el-table>

        <div class="item-pagination">
          <el-pagination
            @current-change="handleCurrentChange"
            :current-page="page.pageNum"
            :page-size="page.pageSize"
            :total="page.total"
            layout="prev, pager, next, jumper, total">
          </el-pagination>
        </div>
      </div>

    </div>

  </div>
</template>

<script type="text/ecmascript-6">
  import { mapState } from 'vuex'
  import ajax from '../../../utils/ajax/index'
  import setDateTime from '../../../utils/date/setDateTime'
  export default {
    data () {
      return {
        activeName: ['1'],
        dateTimeScope: [],
        searchData: { // 搜索字段
          city: 'wuhan',
          aliUserId: '',
          userName: '',
          phone: '',
          identityCard: '',
          beginTime: '',
          endTime: ''
        },
        cityOptions: [
          {label: '武汉', value: 'wuhan'},
          {label: '青岛', value: 'qingdao'},
          {label: '嘉兴', value: 'jiaxing'},
          {label: '泰安', value: 'taian'},
          {label: '石家庄', value: 'shijiazhuang'}
        ],
        table: [] // 表头
      }
    },
    computed: mapState({
      tableTitle: state => state.tableTitle,
      // table: state => state.table,
      tableData: state => state.tableData,
      page: state => state.page
    }),
    mounted () {
      this.Result(this.searchData)
    },
    methods: {
      Result (formData) { // 初始化
        let that = this
        let url = that.$api.WJQUserInfo.result
        let store = that.$store
        let msg = that.$message
        ajax.post(url, formData || {}, store).then((res) => {
          console.log(res)
          let page = res.data.page
          let data = res.data.list
          let tableHeader = res.data.tableHeaders
          that.table = []
          for (let j = 0; j < tableHeader.length; j++) {
            that.table.push({label: tableHeader[j].label, name: tableHeader[j].name})
          }
          data.length === 0 ? msg({type: 'error', message: '未查询到数据'}) : ''
          store.dispatch('setTableData', data)
          store.dispatch('setPage', page)
        })
      },
      search () {
        this.searchData.pageNum = 1
        this.Result(this.searchData)
      },
      reset () {
        this.dateTimeScope = []
        this.$clearJson(this.searchData)
      },
      handleCurrentChange (val) {
        this.searchData.pageNum = val
        this.Result(this.searchData)
      },
      dateTimeScopeChange (val) {
        this.searchData.beginTime = setDateTime(val[0])
        this.searchData.endTime = setDateTime(val[1])
      }
    }
  }
</script>
